////////////////////////////////////////////////////////////////////////////////
//
//  Copyright 2010 galaxy.org
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////

package org.galaxy.richflex.utils
{
	import mx.collections.IList;
	import mx.utils.ObjectUtil;
	import mx.utils.StringUtil;

	/**
	 * String helper class. There are some useful functions about string operations,
	 * such as generates random strings.
	 * 
	 * @author Cheng Liang
	 * 
	 * @version 0.0.1 create 2010-9-17;
	 * @version 0.0.1a renamed from <code>StringUtils</code> to <code>StringHelper</code> 2010-9-27;
	 */
	public class StringHelper
	{
		
		/**
		 * Returns true if the string is null or its trim is empty.
		 * 
		 * @param str string to test
		 * 
		 * @return true if the string is null or its trim is empty.
		 */
		public static function isEmpty(str : String) : Boolean
		{
			return StringUtil.trim(str).length == 0;
		}
		
		/**
		 * Generates random string.
		 * 
		 * @param len generated string length, default is 4
		 * 
		 * @return generated random string
		 */
		public static function randomString(len : int = 4) : String
		{
			var rstr : String = "";
			//get 4 radom
			for(var i:int = 0; i < len; i++) {
				var ran : Number = Math.random();
				var number : Number = Math.round(ran * 10000); //get result like 0.1234
				var code : String;
				if(number % 2 == 0) {
					code = String.fromCharCode(48 + (number % 10)); //0's ASCII code is 48
				} else {
					code = String.fromCharCode(65 + (number % 26)); // A's ASCII code is 65
				}
				rstr += code;
			}
			return rstr;
		}
		
		/**
		 * This will be useful when a string of a list is needed.
		 * 
		 * @param list list to show
		 * @param excludes properties that should not be shown
		 * 
		 * @return string of this list with one item each line
		 */
		public static function listString(list : IList, excludes : Array = null) : String
		{
			var s : String = "";
			for each(var o : Object in list) {
				var claInfo : Object = ObjectUtil.getClassInfo(o, excludes);
				var props : Array = claInfo["properties"];
				for each(var q : QName in props) {
					s += q.localName + ": " + o[q.localName] + "; ";
				}
				s += "\n";
			}
			return s;
		}
		
		public function StringHelper()
		{
		}
		
	}
}